//
//  HUWebView.m
//  WebKit
//
//  Created by Mac on 16/3/14.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "HUWebView.h"
#import "IMYWebView.h"
#import "HUProgress.h"

@interface HUWebView  () <IMYWebViewDelegate>
//@property (nonatomic, strong) UIProgressView* progress;
@property (nonatomic, strong) HUProgress *    progress;
@property (nonatomic, strong) IMYWebView *     webView;
@end

@implementation HUWebView

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame] ;
    if (self) {

        self.webView          = [[IMYWebView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.webView.delegate = self ;
        //监听
        [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
//        [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
        
        [self addSubview:self.webView];
//        self.progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0,
//                                                                              20,
//                                                                              frame.size.width,
//                                                                              5)];
//        self.progress.progressViewStyle = UIProgressViewStyleBar;
//        [self addSubview:self.progress];
        //自定义Layer动画 进度条
        self.progress = [[HUProgress alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 4) withColor:[UIColor colorWithRed:0 green:0.45 blue:1 alpha:1]];
        [self addSubview:self.progress];

    }
    return self;
}
- (void)loadURLString:(NSString *)urlString {
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        
        if (object == _webView) {
            [_progress setAlpha:1.0f];
//            [_progress setProgress:self.webView.estimatedProgress animated:YES];
            [_progress setHUProgress:(CGFloat)self.webView.estimatedProgress];
            if(self.webView.estimatedProgress >= 1.0f) {
                
                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                    [_progress setAlpha:0.0f];
                } completion:^(BOOL finished) {
//                    [_progress :0.0f animated:NO];
                    [_progress setHUProgress:0];
                }];
                
            }
        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
        
    }
//    else if ([keyPath isEqualToString:@"title"])
//    {
//        if (object == self.webView) {
//            self.title = self.webView.title;
//            
//        }
//        else
//        {
//            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
//            
//        }
//    }
    else {
        
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView removeObserver:self forKeyPath:@"title"];
}
- (void)webView:(IMYWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"加载出现错误 ===== %@",error);
}
@end
